
/**
 * 获得当前选择的路径
 */
var getSelectPath = function(){
	var node = $("#tree").tree("getSelected");
	
	return node.id;
}

$(document).ready(function(){
	
	/**
	 * 添加新数据窗口打开
	 */
	var insertNewData = function(){
		$("#insertWindow").window("open");
	}
	
	/**
	 * 查找数据窗口打开
	 */
	var openSearch = function(){
		$("#searchWindow").window("open");
	}
	
	/**
	 * 修改窗口打开
	 */
	var updateIndexData = function(){
		var selectRows = $("#allData").datagrid("getSelections");
		if(selectRows.length != 1){
			$.messager.alert("警告","请选择一行数据！");
		}else{
			$("#updateWindow").window("open");
		}
	}
	
	/** =============== 右边表格 =============== **/
	
	/**
	 * 拼接选择的行的id json
	 */
	var getSelectIds = function(){
		var selectRows = $("#allData").datagrid("getSelections");
		
		var jsonData = "{";
		
		for(var i =0;i<selectRows.length;i++){
			if(i != 0){
				jsonData = jsonData + ",";
			}
			jsonData = jsonData + "\"contentidList[" + i + "]\":" + selectRows[i].contentid;
		}
		jsonData = jsonData + "}";
		
		return jsonData;
	}
	
	/**
	 * 删除
	 */
	var deleteRow = function(){
		var selectRows = $("#allData").datagrid("getSelections");
		if(selectRows.length == 0){
			$.messager.alert("警告","请选择要删除的行！");
		}else{
			$.messager.confirm("警告", "删除之后不可恢复，确定要删除吗？",function(r){
				if (r){
					maskOnLoad("正在删除，请稍后。。。");
					var jsonData = jQuery.parseJSON(getSelectIds());
					var indexPath = getSelectPath();
					
					$.ajax({
						type : "POST",
						dataType : "json",
						url : "luceneAction_luceneDelete?indexPath=" + indexPath,
						data: jsonData,
						success : function(data){
							closeMask();
							if(data.success != null){
								$.messager.alert("成功","删除成功！");
								$("#allData").datagrid("reload");
							}else{
								$.messager.alert("失败",data.error);
							}
						},
						error : function(XMLHttpRequest, textStatus,errorThrown){
							closeMask();
							$.messager.alert("警告","删除失败！");
						}
					});
				}
			});
		}
	}
	
	/**
	 * 此方法在第一次查询时查两次
	 * 可能需要改进
	 * @param {Object} back
	 */
	var loadDataGrid = function(path,page,rows){
		maskOnLoad("正在加载，请稍后。。。");
		$.ajax({
			type : "POST",
			dataType : "json",
			url : "luceneAction_findAllDocuments?indexPath=" + path,
			data: {page: page,rows: rows},//ajax加载时，注意手动添加分页参数
			async : true,
			success : function(back) {
				closeMask();
				$("#allData").datagrid({
					title:"lucene数据",
					width: "auto",
					height: "auto",
					striped: true,	//斑马线
					pageSize : 50,
					pageNumber: 1,
					pageList:[30,50,100],
					rownumbers: true,
					singleSelect: false,	//是否单选
					url: "luceneAction_findAllDocuments?indexPath=" + path,
					pagination: true,	//分页
					frozenColumns : [[	//冻结列
						{field : "ck",checkbox : true},
						{field: "url",title:"url",width:150,
							formatter: function(value,row,index){
								return ("<a href='" + value + "' target='_blank'>" + value + "</a>");
							}
						}
					]],
					columns : back.columns,//列
					queryParams: {
						"queryAttr.fieldName": "",
						"queryAttr.fieldType": "",
						"queryAttr.keyword": ""
					},
					toolbar:[{
						text:"添加",
						iconCls:"icon-add",
						handler:function(){
							insertNewData();
						}
					},{
						text:"修改",
						iconCls:"icon-edit",
						handler:function(){
							updateIndexData();
						}
					},{
						text:"删除",
						iconCls:"icon-cancel",
						handler:function(){
							deleteRow();
						}
					},{
						text:"查找",
						iconCls:"icon-search",
						handler:function(){
							openSearch();
						}
					}]
				});
				
			},
			error : function(XMLHttpRequest, textStatus,errorThrown) {
				closeMask();
				alert(textStatus + "加载失败");
			}
		});
	}
	
	/** =============== 树相关 =============== **/
	$("#tree").tree({
		data: [{
			text: "lucene索引路径",
			state: "closed"
		}],
		lines: true,
		onBeforeSelect: function(node){
			if(node.id == null){//主节点不可选
				return false;
			}
		},
		onSelect: function(selectNode){
			var page = 1;//页数
			var rows = 50;//每页的行数
			loadDataGrid(selectNode.id,page,rows);
		}
	});
	
	$.ajax({
		type : "POST",
		dataType : "json",
		url : "luceneAction_loadTree",
		success : function(data){
			var rootNode = $("#tree").tree("getRoot");
			/* 向root添加子节点 */
			$("#tree").tree("append",{
				parent: rootNode.target,
				data: data
			});
			
			/* 展开所有节点 */
			$("#tree").tree("expandAll");
			
			var children = $("#tree").tree("getChildren",rootNode.target);
			
			/* 选中第一个子节点 */
			$("#tree").tree("select",children[0].target);
		}
	});
});